<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 15-4-1
 * Time: 上午10:08
 * file: SimpleApplication.php
 *
 * Used: Independently execute cli commands
 * class myCommand extends \ulue\core\SimpleApplication
 * {
 *     protected function beginExecute()
 *     {
 *         // do something...
 *     }
 * }
 *
 * call : command line$ ulue command r=module/command;
 *
 */

namespace ulue\cli;

use ulue\core\application;

class SimpleApplication extends application\AbstractApplication
{
    protected $id        = '';  // a namespace classname
    protected $name      = '';  // request name
    protected $classname = '';  // classname|filename , no namespace

    protected $showName  = null;   // show name  e.g. When used in bread crumbs

    protected $path      = '';   // current [module|controller|view(action)] dir path
    protected $route     = '/';   // current [module|controller|view(action)] request route string

    public function init()
    {
        $this->getId();
        $this->getClassname();
    }

    protected function beginExecute()
    {}

    public function setId($value)
    {
        $this->id = $value;

        return $this;
    }

    /**
     * @return string ID of the object,a has namespace's classname
     */
    public function getId()
    {
        if (!$this->id) {
            $this->id = get_class($this);
        }

        return $this->id;
    }

    public function setName($value)
    {
        $this->name = $value;

        return $this;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setClassname($value)
    {
        $this->classname = $value;

        return $this;
    }

    public function getClassname()
    {
        if (!$this->classname) {
            $id = get_class($this);
            $this->classname = strpos($id, '\\') ? basename($id) : $id;
        }

        return $this->classname;
    }

    public function getShowName()
    {
        if ( !$this->showName ) {
            $this->showName = $this->name;
        }

        return $this->showName;
    }

    public function setShowName($name)
    {
        $this->showName = trim($name);

        return $this;
    }

    /**
     * @return string the file dir path
     */
    public function getPath()
    {
        return $this->path;
    }

    public function setPath($value)
    {
        if (!is_dir($value)) {
            throw new \DRuntimeException("请设置一个存在的目录路径");
        }

        $this->path = $value;

        return $this;
    }

    /**
     * 获取文件全路径
     * @return $this
     */
    public function getFile()
    {
        $file = $this->path .'/'.$this->classname. '.php';

        return $file;
    }

    public function getRoute($type=false)
    {
        if ($type) {
            return \Ioc::get('app.uri')->getRoot().'/'.$this->route;
        }

        return $this->route;
    }

    public function setRoute($value)
    {
        $this->route = $value;

        return $this;
    }

}// end class SimpleApplication